// JFC
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
// GTGE
import com.golden.gamedev.*;
import com.golden.gamedev.object.*;
import com.golden.gamedev.util.*;
import com.golden.gamedev.object.background.*;

public class driver extends Game {
    //use for control driver
    
	   
    //tank info
    	public double x = 100;
    	public double y = 200;
    	public int speed=3;
    	private String p= "tank.png";
    	private String PLAYERCENTER="center.png";
    	
    	//ammo info
    		public int ammo=49;
    		private String BULLET="bullet.png";
    		int MAXAMMO=50;
 	   		double center_x=0;
 	   		double center_y=0;
 	   		double bullet_x_origin=0;
 	   		double bullet_y_origin=0;
 	   		double[] bullet_x =  new double[50];
 	   		double[] bullet_y =  new double[50];
 	   		
    public int rotate;
    
    
	SpriteGroup BULLETGROUP, BACKGROUND;
    Sprite  player,playercenter;
    Sprite[] sbullet = new Sprite[50];
   
    BufferedImage tank,pcenter;
    BufferedImage[] bullets=new BufferedImage[50];
    Background background;
    


    
 /****************************************************************************/
 /**************************** GAME SKELETON *********************************/
 /****************************************************************************/

    public void initResources() {
    	 //****************************************************************************/
    	 //**************************** Back Ground Info*******************************/
    	 //****************************************************************************/
    		BACKGROUND= new SpriteGroup("Background Group");
    		
    		background = new  ColorBackground(Color.blue, 1024,768);
    	

    	
 
    	setFPS(45);
    	//****************************************************************************/
    	//****************************Player Info*************************************/
    	//****************************************************************************/
    	//get tank image
    		tank= getImage(p);
    		pcenter= getImage(PLAYERCENTER);
    		

    		
    		player = new Sprite(ImageUtil.rotate(tank,rotate), 100, 200);
    		playercenter= new Sprite(pcenter,100,200);
    		playercenter.setBackground(background);
    		
    		
    		
    		
    		
    		
    		
        //****************************************************************************/
        //****************************Bullet Info*************************************/
        //****************************************************************************/		
    	
    		
    	//load bullet image into bullets array
    		for (int i=0; i<MAXAMMO; i++){
    			
    			bullets[i]=getImage(BULLET);
    			
    		}
    	//load bullets array of bufferd bullet images into sprites
    		
    		sbullet= new Sprite[MAXAMMO];
    		
    		for (int i=0;i<MAXAMMO; i++){
    			sbullet[i]=new Sprite(bullets[i],bullet_x_origin,bullet_y_origin);
    		}
    		
    		
    		BULLETGROUP= new SpriteGroup("Bullet Group");
    		
    		BULLETGROUP.setBackground(background);
    		BULLETGROUP.setActive(false);
    	//load each bullet into BULLETGROUP
    		
    		for(int i=0;i<MAXAMMO;i++){
    			
    			BULLETGROUP.add(sbullet[i]);
    			sbullet[i].setActive(false);
    		}
    		
        	
      
    }


    public void update(long elapsedTime) {
    	
    	
    	//PLAYERGROUP.update(elapsedTime);
    	
        //update(elapsedTime);
    	BULLETGROUP.update(elapsedTime);
        background.update(elapsedTime);
        //player.update(elapsedTime);

        //calculate the center of the player image
        	center_x=(x + 40);
        	center_y=(y + 40);
  	 
        //calculate the origin from which the bullets will be shot from
        	bullet_x_origin= center_x + 30 * Math.cos(( (rotate * Math.PI) )/180);
        	bullet_y_origin= center_y + 30 * Math.sin(( (rotate * Math.PI) )/180);
    
        //sbullet[ammo].setLocation(bullet_x_origin, bullet_y_origin);
        	
        if (keyDown(KeyEvent.VK_LEFT)){
        	
        	rotate=rotate-5;
        	
        	if (rotate <= 0) rotate += 360;
        }
        
        
        if (keyDown(KeyEvent.VK_RIGHT)) {
        	
        	rotate=rotate+5;

        	if (rotate >= 360) rotate =- 360;    
        }

        
        if (keyDown(KeyEvent.VK_UP)){
    	
    		y  =  y +  speed*Math.sin(( (rotate * Math.PI) )/180);
    		
    		x  =  x +  speed*Math.cos(( (rotate * Math.PI) )/180);
    	
        }
       
        if (keyDown(KeyEvent.VK_DOWN)){
    	   
   			y  =  y -  (speed-2)*Math.sin(( (rotate * Math.PI) )/180);
   	  
   			x  =  x -  (speed-2)*Math.cos(( (rotate * Math.PI) )/180);
		
        }
        if (keyDown(KeyEvent.VK_SPACE)) {
        	
        	ammo--;
        	
        	sbullet[ammo].setX(bullet_x_origin);
        	sbullet[ammo].setY(bullet_y_origin);
        	
        	if (ammo<0) ammo=0;
        	
        	if (ammo>0){

        	bullet_x[ammo] =  bullet_x_origin + 50 * Math.cos(( (rotate * Math.PI) )/180);
        	bullet_y[ammo] =  bullet_y_origin + 50 * Math.sin(( (rotate * Math.PI) )/180); 
        
        	}
        	
       }
    	
     //updates image with the rotated one and set it to the new x and y cordinates based upon input from arrows
    	player = new Sprite(ImageUtil.rotate(tank,rotate), x, y);
    	player.setBackground(background);
    	
    //update the center position on tank (***REMEMBER TO TAKE OUT***)	
    	playercenter.setX(center_x);
    	playercenter.setY(center_y);
    	
    
    	
    	//move the bullet to its final place
    	sbullet[ammo].moveTo(elapsedTime, bullet_x[ammo], bullet_y[ammo], 0.5);
    	sbullet[ammo].setActive(true);
    	
    } 


    public void render(Graphics2D g) {
        
    	 
    	background.render(g);
    	player.render(g);
    	//playercenter.render(g);
        BULLETGROUP.render(g);
       
     
    }


 /****************************************************************************/
 /***************************** START-POINT **********************************/
 /****************************************************************************/

    public static void main(String[] args) {
        GameLoader game = new GameLoader();
        game.setup(new driver(), new Dimension(640,480), false);
        game.start();
    }

}